﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NLBS.Entity;
using NLBS.Data;
using System.Text;
using NLBS.Web;
using NLBS.Data.Business;
using NLBS.Views;
using System.Collections.Generic;

namespace NLBS.Control
{
    public partial class comment : BaseUserControl, ICommentListView
    {
        #region Presenter

        private CommentListPresenter _presenter;

        public CommentListPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new CommentListPresenter();
                this._presenter.View = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            if (!this.IsPostBack)
            {
                this.Presenter.OnViewInitialized();
            }
            
            this.Presenter.OnViewLoaded();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            PresenterInit();
            if (!EnableComment)
            {
                ShowError("禁用评论");
                return;
            }
            if (!IsPostBack)
            {
                BindComment();
            }
        }
        void BindComment()
        {
            startIndex = AspNetPager1.StartRecordIndex;
            this.pageSize = AspNetPager1.PageSize;          
            DataHelp.GetComment(AspNetPager1, commentList, CommentList);
        }
        #region event

        protected override void OnInit(EventArgs e)
        {
            commentList.ItemDataBound += new RepeaterItemEventHandler(commentList_ItemDataBound);
            base.OnInit(e);
        }

        void commentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal content = (Literal)e.Item.FindControl("content");
            Comment item = (Comment)e.Item.DataItem;
            content.Text = RenderComment(item);
        }

        #endregion

        public string RenderComment(Comment item)
        {
            return ViewHelp.RenderComment(item);
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindComment();
        }

        #region ICommentListView Members

        private List<Comment> _commentList;
        public List<Comment> CommentList
        {
            get
            {
                return _commentList;
            }
            set
            {
                _commentList=value;
            }
        }
        private bool _enableComment;
        public bool EnableComment
        {
            get
            {
                return _enableComment;
            }
            set
            {
                _enableComment=value;
            }
        }

        public int catID
        {
            get { return RequestWrapper.GetQueryInt("cat"); }
        }

        public int userID
        {
            get { return RequestWrapper.GetQueryInt("user"); }
        }

        private string _pageUrl;
        public string pageUrl
        {
            get
            {
                 return _pageUrl;
            }
            set
            {
                _pageUrl=value;
               
                AspNetPager1.EnableUrlRewriting = true;
                AspNetPager1.UrlRewritePattern = _pageUrl;
                
            }
        }

        public string q
        {
            get { return RequestWrapper.GetQueryString("q"); }
        }

        private int _startIndex;
        public int startIndex
        {
            get
            {
                return _startIndex;
            }
            set
            {
                _startIndex = value;
            }
        }
        private int _pageSize;
        public int pageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {

                _pageSize = value;
            }
        }

        #endregion
    }
}